package cn.kai.system.controller;

import cn.kai.basic.util.AjaxResult;
import cn.kai.basic.util.PageList;
import cn.kai.system.query.SystemDictionaryTypeQuery;
import cn.kai.system.domain.SystemDictionaryType;
import cn.kai.system.service.ISystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Kai
 * @Date 2022/6/5 12:42
 * @PackageName:cn.kai.org.controller
 * @ClassName: SystemDictionaryTypeController
 * @Description: systemdictionarytype表controller层
 * @Version 1.0
 */

@RestController
@RequestMapping("/sdtype")
@Api(value = "数据字典类型的API",description="数据字典类型相关的CRUD功能")
public class SystemDictionaryTypeController {
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;

    /**
     * 基于id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public SystemDictionaryType queryById(@PathVariable("id") Long id){
        return systemDictionaryTypeService.queryById(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    public List<SystemDictionaryType> queryAll(){
        return systemDictionaryTypeService.queryAll();
    }

    /**
     * 高级查询及分页
     * @param query
     * @return
     */
    @PostMapping
    public PageList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery query){
        return systemDictionaryTypeService.queryPage(query);
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            systemDictionaryTypeService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务器繁忙，请稍后重试！");
        }
    }

    /**
     * 新增或修改
     * @param systemDictionaryType
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemDictionaryType){
        try {
            if (systemDictionaryType.getId() == null){
                systemDictionaryTypeService.save(systemDictionaryType);
            }else {
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务器繁忙，请稍后重试！");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            systemDictionaryTypeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务器繁忙，请稍后重试！");
        }
    }
}
